// window.Cesium = Cesium;
import { WebMapTileServiceImageryProvider } from 'cesium';
import * as Cesium from 'cesium';

export const CesiumAccessToken =
  'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI4NWRlNTczMi1kZWRiLTRlZmYtOTMzNC01NmMwNjBiYmIzYmMiLCJpZCI6MTc4MzkyLCJpYXQiOjE3NTYyNTY1MTR9.DKPLFLapcn2ilSm0Pw525QEK3Oqck7ip7Nrds7781p4';

export const MAPBOXGL_ACCESSTOKEN =
  'pk.eyJ1IjoiMzM2Mzg4NTg1NSIsImEiOiJjbG4xZjEzb3EwMDRqMmxubW9xZ3A2MjExIn0.s0BjyQxXURuxGT5dwkkecQ';

Cesium.Ion.defaultAccessToken = CesiumAccessToken;

export const subdomains = ['0', '1', '2', '3', '4', '5', '6', '7'];

export function getTDTkey() {
  const TDT_KEYS = [
    '50f23323fd61ce53465fca366c449e87',
    '9efe144857648780afd069b42769ebe7',
    'f9540e4ba70329698cf2b10338938aa1',
    'c9f1c6dabf6aa551974969ea0a68f4df',
    '359e09124b4b02aeaf02df88f7da6689',
    '1cbdd3829c3991a80b110edbe08b91a7',
    '57afaefd3c351f564d43acfdc422799b',
    '20668bac20071713cb3ed4bc5ca8c798',
  ];

  return TDT_KEYS[Math.floor(Math.random() * TDT_KEYS.length)];
}

/**
 * * 默认地图初始状态
 */
export const DEFAULT_MAP_STATE = {
  // 内蒙古贺兰山
  latitude: 38.758801,
  longitude: 105.863331,
  height: 1000, // 3d下视角默认高度
  zoom: 16,
  cesiumLatitude: 35.19, // cesium camera 纬度
  cesiumHeading: 0, // cesium camera朝向  0 - 正北
  cesiumPitch: -45, // cesium camera俯仰角
  cesiumRoll: 0, // cesium camera翻滚角
  maxZoom: 17,
  minZoom: 9,
  bearing: 0,
  pitch: 0,
};

// 矢量底图
export const CESIUM_TDT_VEC_LAYER = new WebMapTileServiceImageryProvider({
  url: `https://t{s}.tianditu.gov.cn/vec_w/wmts?tk=${getTDTkey()}`,
  format: 'tiles',
  layer: 'vec',
  style: 'default',
  tileMatrixSetID: 'w',
  maximumLevel: 18,
  subdomains: subdomains,
});

// 矢量注记
export const CESIUM_TDT_CVA_LAYER = new WebMapTileServiceImageryProvider({
  url: `https://t{s}.tianditu.gov.cn/cva_w/wmts?tk=${getTDTkey()}`,
  format: 'tiles',
  layer: 'cva',
  style: 'default',
  tileMatrixSetID: 'w',
  maximumLevel: 18,
  subdomains: subdomains,
});

/**
 * 影像图层
 */
export const CESIUM_TDT_IMG_LAYER = new WebMapTileServiceImageryProvider({
  url: `https://t${(Math.random() * (7 - 0 + 1) + 0) | 0}.tianditu.gov.cn/img_w/wmts?tk=${getTDTkey()}`,
  format: 'tiles',
  layer: 'img',
  style: 'default',
  tileMatrixSetID: 'w',
  maximumLevel: 18,
  subdomains: subdomains,
});

// 影像注记
export function addTDT_GOV_Layer(viewer) {
  const tdtNoteLayerProvider = new Cesium.WebMapTileServiceImageryProvider({
    url: `http://t{s}.tianditu.com/cia_w/wmts?service=wmts&tk=${getTDTkey()}&request=GetTile&version=1.0.0&LAYER=cia&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default.jpg`,
    layer: 'tiandituImgMarker',
    style: 'default',
    format: 'image/jpeg',
    tileMatrixSetID: 'tiandituImgMarker',
    show: true,
    subdomains,
    maximumLevel: 16,
  });

  viewer.imageryLayers.addImageryProvider(tdtNoteLayerProvider);
}

// 地形晕染(天地图)
export const CESIUM_TDT_TER_LAYER = new WebMapTileServiceImageryProvider({
  url: `https://t{s}.tianditu.gov.cn/ter_w/wmts?tk=${getTDTkey()}`,
  format: 'tiles',
  layer: 'ter',
  style: 'default',
  tileMatrixSetID: 'w',
  maximumLevel: 18,
  subdomains: subdomains,
});

// 地形晕染标注(天地图)
export const CESIUM_TDT_CTA_LAYER = new WebMapTileServiceImageryProvider({
  url: `https://t{s}.tianditu.gov.cn/cta_w/wmts?tk=${getTDTkey()}`,
  format: 'tiles',
  layer: 'cta',
  style: 'default',
  tileMatrixSetID: 'w',
  maximumLevel: 18,
  subdomains: subdomains,
});

/**
 * mapbox影像底图
 */

export const CESIUM_MAPBOX_IMG_LAYER = new Cesium.UrlTemplateImageryProvider({
  credit: 'mapbox',
  url: `https://api.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}.webp?sku=1016Ab1dNMw2X&access_token=${MAPBOXGL_ACCESSTOKEN}`,
});

/**
 * 0.8m遥感影像
 */
export const CESIUM_KE_HU_IMG_LAYER = new Cesium.UrlTemplateImageryProvider({
  credit: '自定义影像',
  url: `${import.meta.env.VITE_YAO_GAN_LAYER_URL}/nnewtiles/{z}/{x}/{reverseY}.png`,
  // tilingScheme: new Cesium.WebMercatorTilingScheme(),
});
